package com.hcz.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hcz.dao.UserDao;
import com.hcz.entity.User;
import com.hcz.util.Const;
import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String chaptcha = request.getParameter("check");
		if(SimpleImageCaptchaServlet.validateResponse(request, chaptcha)){
			System.out.println("验证正确");
			String name = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			
			UserDao ud = new UserDao();
			User user = ud.selectByNameAndPwd(name, pwd);
			
			//查找到这个用户，
			if(user == null){
				System.out.println("用户不存在");
				response.sendRedirect("index.jsp?code = 1001");//这些都是bug,功能没有实现
				return ;
			}
			
			//System.out.println("OK");
			//用户登录成功，将用户放到session里，
			//功能不完善，就是没有添加过滤器，处理用户登录判断，以及监听器的使用
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_USER,user);
			response.sendRedirect("main.jsp");
		}else {
			
			System.out.println("验证码输入错误：");
			response.sendRedirect("loginIndex.jsp");
			return ;
		}
	}

}
